package rulusoft.nook.rssReader;

import java.io.EOFException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

         public class RssReaderActivity extends Activity {
        	 
			@Override
       	  public void onCreate(Bundle savedInstanceState) {
       	    super.onCreate(savedInstanceState);
                //making activity full screen
                //requestWindowFeature(Window.FEATURE_NO_TITLE);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
                
                //loading main.xml
                setContentView(R.layout.main);
                
                connect = new core();
                if(connect.getAuthStatic()=="") {
                	Handler handler=new Handler();
                	handler.postDelayed(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
		                	Intent intent=new Intent(RssReaderActivity.this, loginActivity.class);
							startActivityForResult(intent, 0);
						}
					}, 0);
                }
                /******************************************************/
                /************ TESTING PARSING AUTH STRING *************/
                /******************************************************/
                
                subsWebView=(WebView)findViewById(R.id.subsWebView);
                testView=(TextView)findViewById(R.id.testView);
                
        		/** Create a new textview array to display the results */
        		
        		
                /** reading authentification code **/
                Button testbtn=(Button)findViewById(R.id.testbtn);
                testbtn.setText("test");
                
                /**********************************************************/
                /************** READING SUBSCRIPTION LIST *****************/
                /**********************************************************/
                
                subsListView=(ListView)findViewById(R.id.subsciptionsList);
                
                subsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						
					}
				});
                
                testbtn.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						subscriptionList();
					}
				});
                                            
                /** EXITING THE APP **/
                exitbtn=(Button)findViewById(R.id.exitbtn);
                exitbtn.setText("exit");
                
                exitbtn.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
						System.exit(0);
					}
				});
                
                /** LOGING IN TO GOOGLE READER **/
                Button loginbtn=(Button)findViewById(R.id.loginbtn);
                loginbtn.setText("Login");
                loginbtn.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent=new Intent("rulusoft.nook.rssReader.login");
						startActivity(intent);						
					}
				});    
                subscriptionList();
        }
			
		public void subscriptionList() {
			String list="";
			String auth=connect.getAuthStatic();
			list=connect.getGoogleSubscriptionList(auth);
			subscriptions subs = new subscriptions();
			
			try {
				SAXParserFactory spf = SAXParserFactory.newInstance();
				SAXParser sp = spf.newSAXParser();
				XMLReader xr = sp.getXMLReader();
				
				InputSource inputSource = new InputSource();
		        inputSource.setEncoding("UTF-8");
		        StringReader str = new StringReader(list);
		        inputSource.setCharacterStream(str);
		        
		        xmlListHandler subsListHandler=new xmlListHandler();
		        xr.setContentHandler(subsListHandler);
		        xr.parse(inputSource);
		        
			} catch (Exception e) {
				System.out.println("XML Pasing Excpetion = " + e);
			}
			
			subs = xmlListHandler.subs;
			
			for(int i=0; i<subs.getTitle().size(); i++) {
				idSubs.add(subs.getId().get(i));
				titleSubs.add(subs.getTitle().get(i));
			}
			//testView.setText(text);
            aa = new ArrayAdapter<String>(this, R.layout.list_item, titleSubs);
            subsListView.setAdapter(aa);
		}
       
        private WebView subsWebView;
        private String auth="";
        private Button exitbtn;
        public static RssItem selectedRssItem = null;
        String feedUrl = "";
        ListView rssListView = null;
        public ArrayList<RssItem> rssItems = new ArrayList<RssItem>();
        public ArrayList<String> idSubs = new ArrayList<String>();
        public ArrayList<String> titleSubs = new ArrayList<String>();
        public ArrayAdapter<String> listSubs = null;
        ArrayAdapter<String> aa = null;
        public core connect;
        TextView testView;
        ListView subsListView;

}
